<template>
  <div class="info-message">
    <Headline
      :headline1="$t('m.MessageApplication') + '  /  '"
      :headline2="$t('m.WeChatUser')"
    />
    <a-card style="width: 100%">
      <a-select default-value="-1" style="width: 120px" @change="handleChange">
        <a-select-option value="-1"> 全部 </a-select-option>
        <a-select-option value="1"> 学生 </a-select-option>
        <a-select-option value="2"> 家长 </a-select-option>
        <a-select-option value="3"> 职工 </a-select-option>
      </a-select>
      <a-input-search
        style="width: 310px; margin-bottom: 30px; margin-left: 15px"
        :placeholder="$t('m.PleaseEnterYourSearchTerms')"
        :enter-button="$t('m.Inquire')"
        size="default"
        v-model="forminitial.key"
        @search="infoMessageSearch"
      />
      <div style="float: right">
        <a-button icon="sync" @click="refresh">
          {{ $t("m.refresh") }}
        </a-button>
      </div>
      <a-table
        :rowKey="(record) => record.id"
        :data-source="data"
        :columns="columns"
        :pagination="false"
        bordered
      >
        <img
          style="width: 50px; heigth: 50px; border-radius: 50%"
          slot="pic"
          slot-scope="text, record"
          :src="record.headimgurl"
        />
        <div slot="index" slot-scope="text, tags, i">
          {{ i + 1 + (forminitial.page - 1) * forminitial.limit }}
        </div>
        <div slot="status" slot-scope="text, tags, i">
          <a-switch
            :disabled="!isTrue('msg:wechat:user:valid')"
            :checked="tags.status == 0 ? false : true"
            @change="onChange(text, tags, i)"
          />
        </div>
        <div slot="del" slot-scope="text, tags, i">
          <a-button
            icon="delete"
            type="danger"
            :disabled="!isTrue('msg:wechat:user:del')"
            @click="delBlackName(text, tags, i)"
            >{{ $t("m.Delete") }}</a-button
          >
        </div>
      </a-table>
      <a-pagination
        style="margin-top: 20px"
        show-size-changer
        :default-current="1"
        :total="total"
        @change="Pages"
        :pageSizeOptions="['5', '10', '30', '40']"
        @showSizeChange="onShowSizeChange"
        class="pages"
      />
    </a-card>
    <Delete ref="unitdelete" :dalete="dalete" @confirm="delIsok"></Delete>
    <Delete ref="edit" :dalete="daletes" @confirm="editData"></Delete>
  </div>
</template>

<script>
import Headline from "@/components/Headline";
import {
  getWeiXinUserList,
  setWeiXinUser,
  delWeiXInUser,
} from "@/api/information";
import Delete from "@/components/Delete";
import { Setperms } from "@/assets/js/setperms";
export default {
  name: "InfoMessage",
  components: { Headline, Delete },
  data() {
    return {
      data: [],
      dalete: {
        titles: this.$t("m.DeleteThePrompt"),
        type: "exclamation-point",
        text: this.$t("m.AreYouSureToDeleteThisData"),
      },
      daletes: {
        titles: this.$t("m.ChangeStatus"),
        type: "exclamation-point",
        text: this.$t("m.AreYouSureAboutTheModificationStatus"),
      },
      columns: [
        {
          title: this.$t("m.SerialNumber"),
          scopedSlots: { customRender: "index" },
        },
        {
          title: this.$t("m.WeChatAatar"),
          dataIndex: "headimgurl",
          scopedSlots: { customRender: "pic" },
        },
        { title: this.$t("m.WeChatNickname"), dataIndex: "nickname" },
        { title: this.$t("m.Name"), dataIndex: "stafforstuname" },
        { title: this.$t("m.jobnumber"), dataIndex: "stafforstucode" },
        { title: this.$t("m.AffiliatedUnit"), dataIndex: "custorgname" },
        { title: this.$t("m.Sex"), dataIndex: "sex" },
        { title: this.$t("m.UserType"), dataIndex: "usertype" },
        {
          title: this.$t("m.AccountStatus"),
          dataIndex: "status",
          scopedSlots: { customRender: "status" },
        },
        //{ title: "账号状态", dataIndex: "status" },
        {
          title: this.$t("m.PrivateDateLastlogintime"),
          dataIndex: "lastlogintime",
        },
        {
          title: this.$t("m.Operation"),
          scopedSlots: { customRender: "del" },
        },
      ],
      total: 0,
      forminitial: {
        start: 0,
        limit: 10,
        key: "",
        userType: -1,
        page: 1,
      },
      delId: "",
      ids:""
    };
  },
  created() {
        
    this.getData();
    this.parms = Setperms(
      this.$route.fullPath,
      JSON.parse(sessionStorage.getItem("menu"))
    );
    console.log(this.parms);
  },

  methods: {
    isTrue(str) {
      return this.parms.indexOf(str) !== -1;
    },
    delIsok() {
      delWeiXInUser({ ids: this.delId })
        .then((res) => {
          if (res.data.success == false) {
            this.$message.error("删除失败");
          }
          if (res.data.success == true) {
            this.$message.success("删除成功");
          }
          this.getData();
        })
        .catch((err) => {
          this.$message.error("操作失败");
        });
      this.$refs.unitdelete.visible = false;
    },
    delBlackName(text, tags, i) {
      this.$refs.unitdelete.visible = true;
      this.delId = tags.id;
    },
    onChange(text, tags, i) {
      this.$refs.edit.visible = true
       this.ids = tags.id
    },
    editData() {
      setWeiXinUser({ ids: this.ids })
        .then((res) => {
          console.log(res);
          if (res.data.success == true) {
            this.$message.success(res.data.msg);
            this.getData();
          }
          if (res.data.success == false) {
            this.$message.error(res.data.msg);
          }
        })
        .catch((err) => {
          this.$message.error("操作失败");
        });
        this.$refs.edit.visible = false
    },
    refresh() {
      this.getData();
    },
    onShowSizeChange(page, limit) {
      this.forminitial.page = page;
      this.forminitial.limit = limit;
      this.getData();
    },
    Pages(page, pageSize) {
      this.forminitial.limit = pageSize;
      this.forminitial.page = page;
      this.forminitial.start = (page - 1) * pageSize;
      this.getData();
    },
    handleChange(value) {
      this.forminitial.userType = value;
      this.getData();
    },
    infoMessageSearch() {
      this.getData();
    },
    getData() {
      getWeiXinUserList({ params: this.forminitial })
        .then((res) => {
          if (res.data.success == true) {
            this.$message.success("获取数据成功");
            this.data = res.data.data;
            this.data.forEach((item) => {
              if (item.usertype == 1) {
                item.usertype = "学生";
              }
              if (item.usertype == 2) {
                item.usertype = "家长";
              }
              if (item.usertype == 3) {
                item.usertype = "职工";
              }
              if (item.sex == 1) {
                item.sex = "男";
              }
              if (item.sex == 2) {
                item.sex = "女";
              }
            });
            this.total = res.data.total;
          }
          if (res.data.success == false) {
            this.$message.error(res.data.msg);
          }
        })
        .catch((err) => {
          this.$message.error("获取数据失败");
        });
    },
  },
  computed: {},
};
</script>

<style lang="scss" scoped>
</style>
